﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Workflow.Runtime.Hosting;
using SharedWorkflows;
using System.Workflow.Activities;
using System.Data.SqlClient;
using CommonLibrary.DataModel.Basic;
using ServiceApplication.Dao.Basic;

namespace WinformTest
{
    public partial class CreateBill : Form
    {
        UserDao userDao = new UserDao();
        public CreateBill(Guid guid)
        {
            InitializeComponent();
            this.textBoxId.Text = guid.ToString();
            RetrieveExistingWorkflows();
        }

        #region Collection Management

        private void RetrieveExistingWorkflows()
        {
            GlobleVar._workflows.Clear();
            foreach (SqlPersistenceWorkflowInstanceDescription workflowDesc
                in ((SqlWorkflowPersistenceService)GlobleVar._persistence).GetAllWorkflows())
            {
                Workflow workflow = new Workflow();
                workflow.InstanceId = workflowDesc.WorkflowInstanceId;
                workflow.StatusMessage = "Unloaded";
                GlobleVar._workflows.Add(workflow.InstanceId, workflow);
            }

            if (GlobleVar._workflows.Count > 0)
            {
                RefreshData();
            }
        }

        private void RefreshData()
        {
            if (dataGridView1.DataSource == null)
            {
                //setup binding for DataGridView the first time
                dataGridView1.DataSource = new BindingList<Workflow>();

                dataGridView1.Columns[0].MinimumWidth = 250;
                dataGridView1.Columns[1].MinimumWidth = 140;
                dataGridView1.Columns[2].MinimumWidth = 40;
            }

            BindingList<Workflow> bl =
                (BindingList<Workflow>)dataGridView1.DataSource;
            foreach (Workflow wf in GlobleVar._workflows.Values)
            {
                //make sure the bindingList contains all of the 
                //workflow instances
                if (!bl.Contains(wf))
                {
                    bl.Add(wf);
                    //set the new instance as the current 
                    //one in the grid view
                    this.BindingContext[dataGridView1.DataSource].Position
                        = bl.Count - 1;
                }
            }

            dataGridView1.Refresh();
        }

        #endregion

        #region UIevent
        private void button_ok_Click(object sender, EventArgs e)
        {
            #region 实例值操作
            Guid InstanceId = new Guid(textBoxId.Text);
            BillModel model = new BillModel();
            model.BillId = textBoxId.Text;
            model.Money = decimal.Parse(textBoxMoney.Text);
            model.UserName = textBoxPeople.Text;
            model.UserID = GlobleVar.mySelf.id;

            String sql = "insert into BillModel(UserID,UserName,Money,Fremark,Bremark,Premark,LastRemark,InstanceID) " +
                "values(@UserID,@UserName,@Money,@Fremark,@Bremark,@Premark,@LastRemark,@InstanceID)";
            List<SqlParameter> sqlparams = new List<SqlParameter>();
            sqlparams.Add(new SqlParameter("@UserID", model.UserID));
            sqlparams.Add(new SqlParameter("@UserName", model.UserName));
            sqlparams.Add(new SqlParameter("@Money", model.Money));
            sqlparams.Add(new SqlParameter("@Fremark", ""));
            sqlparams.Add(new SqlParameter("@Bremark", ""));
            sqlparams.Add(new SqlParameter("@Premark", ""));
            sqlparams.Add(new SqlParameter("@LastRemark", ""));
            sqlparams.Add(new SqlParameter("@InstanceID", textBoxId.Text));
            CommonLibrary.DB.DBExecuteUtil.executeSqlWithPar(sql, sqlparams.ToArray());

            //if (model.Money > 1000)
            //{
            //    GlobleVar._billServer.onBillInitMoneyMax(
            //        new ExternalDataEventArgs(InstanceId), GlobleVar.mySelf, new int[] { 2 }, "老板",null);
            //}
            //else if (model.Money < 1000)
            //{
            //    GlobleVar._billServer.onBillInitMoneyMin(
            //        new ExternalDataEventArgs(InstanceId), GlobleVar.mySelf, new int[] { 7 }, "财务",null);
            //}
            #endregion


            MessageBox.Show("OK");
            this.Close();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
                GlobleVar._selectedWorkflow = selectedRow.DataBoundItem as Workflow;
            }
        }
        #endregion
    }
}
